//********************************************************************
// 
//  file:  AccountManagementSample.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using data.diomedestorage.com;
using Diomede.SDK.Enums;

namespace Diomede.SDK.Example.Samples
{
    public class AccountManagementSample
    {
        public void Start()
        {
            try
            {
                Console.WriteLine("\n\n");
                CreateAccountSample();
                SetAccountInfoSample();
                GetAccountInfoSample();
                AddEmailAddressSample();
                SetPrimaryEmailAddressSample();
                DeleteAccountInfoSample();
                GetEmailAddressSample();
                GetEmailAddressListSample();
                RemoveEmailAddressSample();
                SearchLoginLogs();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Utils.WriteMessage("Press any key...");
            Console.ReadKey();
            
        }


        #region examples

        static string username = string.Format("user_{0}", DateTime.Now.Ticks);
        static string password = "password914";
        static string email = string.Format("{0}@example.com", username);

        static void CreateAccountSample()
        {
            ISession session = new Session();
            IAccountManagement accountManager = session as IAccountManagement;
            if (accountManager.Create(username, password, email).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to create account.");
            }
            else
            {
                Console.WriteLine("Account successfully created.");
            }
        }

        static void SetAccountInfoSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fail to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;
            //create userinfo
            UserInfo userInfo = new UserInfo();
            userInfo.firstName = "Melvin";
            userInfo.lastName = "Sembrano";
            userInfo.companyName = "MS Company";
            userInfo.websiteUrl = "www.melvin.com";
            userInfo.cardName = "AMelvin Sembrano";
            userInfo.cardNumber = "1234567890123456";
            userInfo.cardCvv2 = "023";
            userInfo.cardAddress1 = "123 Address 1-A";
            userInfo.cardAddress2 = "321 Address 2-B";
            userInfo.cardCity = "Cebu";
            userInfo.cardState = "Cebu P";
            userInfo.cardCountry = "63";
            userInfo.cardZip = "6000";

            if (accountManager.UpdateAccountInfo(userInfo).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to update account information.");
            }
            else
            {
                Console.WriteLine("Successfully update the user info.");
            }

            session.Logout();
        }

        static void GetAccountInfoSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;

            ISessionExecResult result = accountManager.GetAccountInfo();
            if (result.Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to get user's account information.");
            }
            UserInfo userInfo = result.UserInfo;
            Console.WriteLine(string.Format("{0} {1}'s information.",userInfo.firstName, userInfo.lastName));
        }

        static void DeleteAccountInfoSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fail to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;
            //delete the current billing info
            if (accountManager.DeleteAccountInfo(UserInfoType.allBillingInfo).Status != ExecutionResults.Success)
            {
                throw new Exception("Fail to delete billing information.");
            }
            else
            {
                Console.WriteLine("Successfully deleted the account billing information.");
            }
        }

        static void AddEmailAddressSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;

            if (accountManager.AddEmail(string.Format("{0}_second@sample.com", username)).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to add email address.");
            }
            else
            {
                Console.WriteLine("Successfully added email address.");
            }
        }

        static void GetEmailAddressSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;

            if (accountManager.GetEmailAddresses().Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to retrieve email addresses.");
            }
            else
            {
                Console.WriteLine("Successfully retrieved email addresses.");
            }
        }

        static void GetEmailAddressListSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;

            if (accountManager.GetEmailAddressList() == null)
            {
                throw new Exception("Fails to retrieve email addresses list.");
            }
            else
            {
                Console.WriteLine("Successfully retrieved email addresses list.");
            }
        }

        static void RemoveEmailAddressSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;

            if (accountManager.RemoveEmailAddress(string.Format("{0}_second@sample.com", username)).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to remove email address.");
            }
            else
            {
                Console.WriteLine("Successfully removed email address.");
            }
        }

        static void SearchLoginLogs()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            ILogsManagement logManager = session as ILogsManagement;

            if (logManager.SearchLoginLogs(null, null, string.Empty, 0, 100).Status != ExecutionResults.Success)
            {
                throw new Exception("Search on login logs fails.");
            }
            else
            {
                Console.WriteLine("Search on login logs successful.");
            }
        }

        static void SetPrimaryEmailAddressSample()
        {
            ISession session = new Session();
            if (session.Login(username, password).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to login.");
            }

            IAccountManagement accountManager = session as IAccountManagement;

            if (accountManager.AddEmail(string.Format("{0}_newprimary@sample.com", username)).Status != ExecutionResults.Success)
            {
                throw new Exception("Fails to set new primary email address.");
            }
            else
            {
                Console.WriteLine("Successfully set primary email address.");
            }
        }

        #endregion
    }
}
